//
//  FR_Late_Fitter_ViewController.m
//  FaceRecognition
//
//  Created by zz on 17/7/14.
//  Copyright © 2017年 Sylar. All rights reserved.
//

#import "FR_Late_Fitter_ViewController.h"
#import "DateView.h"
#import "ManagerInfoModel.h"
#import "FR_ClassesPicker.h"

@interface FR_Late_Fitter_ViewController ()  <UITableViewDelegate,UITableViewDataSource,UIPickerViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

/**选择班级按钮*/
@property (nonatomic, strong) UIButton *selClassBtn;

/**开始时间按钮*/
@property (nonatomic, strong) UIButton *selStartTimeBtn;

/**结束时间按钮*/
@property (nonatomic, strong) UIButton *selEndTimeBtn;

/**搜索学生姓名*/
@property (nonatomic, strong) UITextField *searchNameField;

@property (nonatomic, strong) NSMutableArray *dataArr;

/**选择的班级字符串*/
@property (nonatomic, copy) NSString *selClassStr;

/**选择的班级id字符串*/
@property (nonatomic, copy) NSString *selClassIdStr;

/**开始时间字符串*/
@property (nonatomic, copy) NSString *selDateStartStr;
/**开始时间字符串*/
@property (nonatomic, copy) NSString *selDateEndStr;



@end

@implementation FR_Late_Fitter_ViewController

#pragma mark -Getter
- (UITableView *)tableView {
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight, VIEW_WIDTH, 213) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [UIView new];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        _tableView.scrollEnabled = false;
        
        _tableView.rowHeight = 44;
        
    }
    
    return _tableView;
}



#pragma mark -LifeCycle
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithHexString:@"#f4f5f7"];
    
    if (!_navTitle) {
        _navTitle = @"筛选";
    }
    
    self.navBar.nav_titleLabel.text = _navTitle;
    [self.view addSubview:self.tableView];

   self.navBar.rightButton.hidden = NO;
   [self.navBar.rightButton setTitle:@"清空 " forState:UIControlStateNormal];
   [self.navBar.rightButton setTitleColor:[UIColor colorWithHexString:@"#1098fd"] forState:UIControlStateNormal];
   self.navBar.rightButton.titleLabel.font = [UIFont systemFontOfSize:17];
   [self.navBar.rightButton addTarget:self action:@selector(clearAllAction) forControlEvents:UIControlEventTouchUpInside];
   
    
    //获取班主任所管理班级
    [self getManagerClasses];
    
    [self initSubViews];
}

- (void)initSubViews {
    
    CGFloat rate;
    
    if (SCREENWIDTH == 414) {
        rate = 10;
    }
    
    //选择年级
    _selClassBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _selClassBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [_selClassBtn setTitle:@"请选择" forState:UIControlStateNormal];
    
    [_selClassBtn addTarget:self action:@selector(selClassAction:) forControlEvents:UIControlEventTouchUpInside];
    [_selClassBtn setTitleColor:[UIColor colorWithHexString:@"#b4b4b4"] forState:UIControlStateNormal];
    _selClassBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [_selClassBtn setFrame:CGRectMake(VIEW_WIDTH - 25 - 150 - rate, 0, 150, 44)];
    
    //选择起始时间
    _selStartTimeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _selStartTimeBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [_selStartTimeBtn setTitle:@"请选择" forState:UIControlStateNormal];
    [_selStartTimeBtn setTitleColor:[UIColor colorWithHexString:@"#b4b4b4"] forState:UIControlStateNormal];
    [_selStartTimeBtn addTarget:self action:@selector(selStateTimeAction) forControlEvents:UIControlEventTouchUpInside];
    _selStartTimeBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [_selStartTimeBtn setFrame:CGRectMake(VIEW_WIDTH - 25 - 150 - rate, 0, 150, 43.5)];
    
    //选择结束时间
    _selEndTimeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _selEndTimeBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [_selEndTimeBtn setTitle:@"请选择" forState:UIControlStateNormal];
    [_selEndTimeBtn setTitleColor:[UIColor colorWithHexString:@"#b4b4b4"] forState:UIControlStateNormal];
    [_selEndTimeBtn addTarget:self action:@selector(selEndTimeAction) forControlEvents:UIControlEventTouchUpInside];
    [_selEndTimeBtn setFrame:CGRectMake(VIEW_WIDTH - 25 - 150 -rate , 0, 150, 44)];
    _selEndTimeBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    
    _searchNameField = [[UITextField alloc] initWithFrame:CGRectMake(VIEW_WIDTH - 25 - 105 - rate, 12, 105, 20)];
    _searchNameField.textColor = [UIColor colorWithHexString:@"#b4b4b4"];
    _searchNameField.textAlignment = NSTextAlignmentRight;
    _searchNameField.placeholder = @"请填写姓名";
    
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [confirmBtn setFrame:CGRectMake(28, CGRectGetMaxY(self.tableView.frame) + 80, VIEW_WIDTH - 28 * 2, 38)];
    [confirmBtn setBackgroundColor:RGBCOLOR(22, 130, 252)];
    confirmBtn.layer.cornerRadius = 4;
    [confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
    [confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    confirmBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [confirmBtn addTarget:self action:@selector(confrimAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:confirmBtn];
    
    //赋值
    NSMutableDictionary *dict;
    
    if (_approvel == -1) { //所有
    
        dict = [SYAccountTool ShareAccount].attendance_dict;
    }else if (_approvel == 0) {
    
        dict = [SYAccountTool ShareAccount].absent_dict;
        
    }else if (_approvel == 2) {
    
        dict = [SYAccountTool ShareAccount].late_dict;
    }
    
    if (dict) {
        
        _selClassStr = [dict objectForKey:@"_selClassStr"];
        _selDateStartStr = [dict objectForKey:@"_selDateStartStr"];
        _selDateEndStr = [dict objectForKey:@"_selDateEndStr"];
        
        _selClassIdStr = [dict objectForKey:@"_selClassIdStr"];
        
        if (_selClassStr.length > 0) {
            
            [_selClassBtn setTitle:_selClassStr forState:UIControlStateNormal];
        }
        
        if (_selDateStartStr.length > 0) {
            
            [_selStartTimeBtn setTitle:_selDateStartStr forState:UIControlStateNormal];
        }
        
        if (_selDateEndStr.length > 0) {
            
            [_selEndTimeBtn setTitle:_selDateEndStr forState:UIControlStateNormal];
        }
        
        _searchNameField.text = [dict objectForKey:@"_selStuName"];
    }
}

#pragma mark -UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        
        return 1;
    }else if (section == 1){
        
        return 2;
    }else {
        
        return 1;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 14, 100, 16)];
    label.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
    label.font = [UIFont systemFontOfSize:16];
    [cell.contentView addSubview:label];
    
    if (indexPath.section == 0 && indexPath.row == 0) {
        label.text = @"选择班级";
        [cell.contentView addSubview:_selClassBtn];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }else if (indexPath.section == 1 && indexPath.row == 0) {
        label.text = @"起始时间";
        
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(15, 43.5, VIEW_WIDTH - 15, 0.5)];
        lineView.backgroundColor = [UIColor colorWithHexString:@"#dedede"];
        [cell.contentView addSubview:lineView];
        
        [cell.contentView addSubview:_selStartTimeBtn];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }else if (indexPath.section == 1 && indexPath.row == 1) {
        label.text = @"结束时间";
        [cell.contentView addSubview:_selEndTimeBtn];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }else {
        label.text = @"宝宝姓名";
        [cell.contentView addSubview:_searchNameField];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

#pragma mark -UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 12;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * lineview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 12)];
    lineview.backgroundColor = [UIColor colorWithHexString:@"#f4f5f7"];
    return lineview;
}

#pragma mark -Private
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [self.view endEditing:YES];
}

#pragma mark -Request
//获取老师身份下所有管理班级(作为班主任的班级)
- (void)getManagerClasses{
    
    NSMutableDictionary * paramDic = [[NSMutableDictionary alloc] initWithCapacity:0];
    [paramDic setObject:USERID forKey:@"teacherid"];
    [paramDic setObject:SCHOOLID forKey:@"scid"];
    
    NSString *type = _managerType == 1 ? @"1" : @"0";
    
    [paramDic setObject:type forKey:@"type"];
    
    [Utils Java_get:@"api/getTeacherClass" parameters:paramDic success:^(NSInteger recode, id jsonObject) {
        
        NSArray *array = [jsonObject objectForKey:@"re_data"];
        
        [self.dataArr removeAllObjects];
        
        if (array.count == 0) {

            return;
        }
        
        self.dataArr = [ManagerInfoModel mj_objectArrayWithKeyValuesArray:array];
        
    } failure:^(NSInteger recode, NSError *error) {
        
        [self showHint:error.localizedDescription];
    }];

}

#pragma mark -Action
//确定
- (void)confrimAction {
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:_selClassStr forKey:@"_selClassStr"];
    [dict setObject:_selDateStartStr forKey:@"_selDateStartStr"];
    [dict setObject:_selDateEndStr forKey:@"_selDateEndStr"];
    [dict setObject:_searchNameField.text forKey:@"_selStuName"];
    [dict setObject:_selClassIdStr forKey:@"_selClassIdStr"];
    
    if (_approvel == -1) {  //考勤
        
        [SYAccountTool ShareAccount].attendance_dict = dict;
    }else if (_approvel == 0) { //旷课
    
        [SYAccountTool ShareAccount].absent_dict = dict;
        
    }else if (_approvel == 2) { //迟到
    
        [SYAccountTool ShareAccount].late_dict = dict;
    }
    
    if (_confirmBlock) {
        
        _confirmBlock(dict);
        [self.navigationController popViewControllerAnimated:YES];
    }
}

//清除所有
- (void)clearAllAction {
    
    _selClassStr = @"";
    _selDateStartStr = @"";
    _selDateEndStr = @"";
    _selClassIdStr = @"";
    
    [_selClassBtn setTitle:@"请选择" forState:UIControlStateNormal];
    [_selStartTimeBtn setTitle:@"请选择" forState:UIControlStateNormal];
    [_selEndTimeBtn setTitle:@"请选择" forState:UIControlStateNormal];
    _searchNameField.text = @"";
    
}

//选择班级
- (void)selClassAction:(UIButton *)sender {

    FR_ClassesPicker *picker = [FR_ClassesPicker showWithSuperView:self.view];
    picker.dataArr = self.dataArr;
    picker.confirmBlock = ^(NSString *nameStr,NSString *idStr) {
        
        if (nameStr.length == 0) {
            
            [_selClassBtn setTitle:@"请选择" forState:UIControlStateNormal];
            _selClassStr = nil;
            _selClassIdStr = @"0";
            return;
        }
    
        if (nameStr.length > 0) {
            
            [_selClassBtn setTitle:nameStr forState:UIControlStateNormal];
            _selClassStr = nameStr;
        }
        
        _selClassIdStr = idStr;
    };
    [self.view addSubview:picker];
}

//选择开始时间
- (void)selStateTimeAction {
    
    [self.view endEditing:YES];
    
    DateView *dateV = [[DateView alloc] initAddSuperView:self.view canceltype:2];
    
    [dateV setGetDate:^(NSString *DateStr) {
        
        if (DateStr.length == 0) { //清空
            
            [_selStartTimeBtn setTitle:@"请选择" forState:UIControlStateNormal];
            _selDateStartStr = @"";
            return;
        }
        
        DateStr = [DateStr substringToIndex:10];
        
        
        
        NSDate *date = [NSDate date];
        NSInteger nowDateInteger = [date timeIntervalSince1970];
        
        NSUInteger startTime = [Utils transformStringToNsinterger:_selDateStartStr];
        _selDateStartStr = DateStr;
        if (startTime > nowDateInteger) {
            
            [Utils alertViewWithMessage:@"起始时间不能大于当前时间"];
            return;
        }
        
        if (_selDateEndStr.length > 0) {
            
            NSUInteger endTime = [Utils transformStringToNsinterger:_selDateEndStr];
            
            if (startTime > endTime) {
                [Utils alertViewWithMessage:@"起始时间不能大于结束时间"];
                _selDateEndStr = nil;
                return;
            }
        }
        
        [_selStartTimeBtn setTitle:DateStr forState:UIControlStateNormal];
        
    }];
}

- (void)selEndTimeAction {

    [self.view endEditing:YES];
    
    DateView *dateV = [[DateView alloc] initAddSuperView:self.view canceltype:2];
    
    [dateV setGetDate:^(NSString *DateStr) {
        
        if (DateStr.length == 0) { //清空
            
            [_selEndTimeBtn setTitle:@"请选择" forState:UIControlStateNormal];
            _selDateEndStr = @"";
            return;
        }
        
        DateStr = [DateStr substringToIndex:10];
        
        _selDateEndStr = DateStr;
        
        if (_selDateStartStr.length > 0) {
            NSUInteger startTime = [Utils transformStringToNsinterger:_selDateStartStr];
            NSUInteger endTime = [Utils transformStringToNsinterger:_selDateEndStr];
            
            if (startTime > endTime) {
                [Utils alertViewWithMessage:@"起始时间不能大于结束时间"];
                _selDateEndStr = nil;
                return;
            }
        }
        
        
        
        [_selEndTimeBtn setTitle:DateStr forState:UIControlStateNormal];
        
    }];
}



@end
